package org.springframework.example.aop;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.example.aop.advice.MyAfterRuturnAdvice;
import org.springframework.example.aop.service.AccountService;

import java.lang.reflect.Method;

public class Test3 {

	public static void main(String[] args) {
		AccountService target = new AccountService();
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(target);
		proxyFactory.addAdvisor(new PointcutAdvisor() {
			@Override
			public Pointcut getPointcut() {
				return new StaticMethodMatcherPointcut() {
					@Override
					public boolean matches(Method method, Class<?> targetClass) {
						return method.getName().equals("test");
					}
				};
			}

			@Override
			public Advice getAdvice() {
				return new MyAfterRuturnAdvice();
			}

			@Override
			public boolean isPerInstance() {
				return false;
			}
		});

		AccountService proxy = (AccountService) proxyFactory.getProxy();
		proxy.test();


	}
}
